import axios from 'axios'
import {apiUrl, componentUrl, ompUrl, config} from '@/configs'

let excGet = (httpAxios) => {
  // 重置了GET方法，包装params
  httpAxios.oldGet = httpAxios.get
  httpAxios.oldDelete = httpAxios.delete
  httpAxios.get = (url, data) => {
    return httpAxios.oldGet(url, {params: data})
  }
  httpAxios.delete = (url, data) => {
    return httpAxios.oldDelete(url, {data: data})
  }
}
const http = axios.create({
  'baseURL': apiUrl,
  'headers': {
    'Content-Type': 'application/json;charset=UTF-8',
    'source': '501',
    'orgId': '18f923a7-5a5e-426d-94ae-a55ad1a4b240',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
    'token': 'AAAAAFdI3Tb4No2nu6gk8VViJuo0cDjgNHrWeMG7RnGHDiODD6H68Zd3EHxmjzMO9Q7Yv8lQ6Y5aYav8P5hXjqZ5KGe4xdsDXjnkPowXgRCNAE1sbE6-ynYJHi960S10D739ogYgLkQoB0RViuCSzJE6Lk8'
  },
  validateStatus: (status) => {
    return status < 400
  }
})
const componentHttp = axios.create({
  'baseURL': componentUrl,
  'headers': {
    'Content-Type': 'application/json;charset=UTF-8',
    'source': '501',
    'orgId': '18f923a7-5a5e-426d-94ae-a55ad1a4b240',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
  },
  validateStatus: (status) => {
    return status < 400
  }
})
const ompHttp = axios.create({
  'baseURL': ompUrl,
  'headers': {
    'Content-Type': 'application/json;charset=UTF-8',
    'source': '501',
    'orgId': '18f923a7-5a5e-426d-94ae-a55ad1a4b240',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
  },
  validateStatus: (status) => {
    return status < 400
  }
})
const loghttp = axios.create({
  'baseURL': apiUrl.log,
  'headers': {
    'Content-Type': 'application/json;charset=UTF-8',
    'source': '501'
  },
  validateStatus: (status) => {
    return status < 400
  }
})
const fileDownHttp = axios.create({
  'baseURL': componentUrl,
  'headers': {
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
    'Accept-Language': 'en-us,en;q=0.5',
    'Content-Type': 'application/json',
    'Source': '101',
    'AppId': 'crm'
  },
  validateStatus: (status) => {
    return status < 400
  }
})
const ajaxInvokeFileHttp = axios.create({
  'baseURL': apiUrl,
  'headers': {
    'Content-Type': 'application/json;charset=UTF-8',
    'source': '501',
    'orgId': '18f923a7-5a5e-426d-94ae-a55ad1a4b240'
  },
  validateStatus: (status) => {
    return status < 400
  }
})

excGet(http)
excGet(loghttp)
excGet(componentHttp)
excGet(fileDownHttp)
const req = {
  'resolve': (config) => {
    return config
  },
  'reject': (error) => {
    // eslint-disable-next-line
    return Promise.reject('req', error)
  }
}

const res = {
  'resolve': (response) => {
    if (response.status === 201) {
      response.data = response.data || {}
      response.data.Location = response.headers.location
    }
    return response.data
  },
  'reject': (error) => {
    if (error.response.status === 401) {
      // eslint-disable-next-line
      return Promise.reject(null)
    }
    // eslint-disable-next-line
    return Promise.reject(error.response.data)
  }
}

// Add a request interceptor
http.interceptors.request.use(req.resolve, req.reject)
ajaxInvokeFileHttp.interceptors.request.use(req.resolve, req.reject)
ompHttp.interceptors.request.use(req.resolve, req.reject)
loghttp.interceptors.request.use(req.resolve, req.reject)
componentHttp.interceptors.request.use(req.resolve, req.reject)
fileDownHttp.interceptors.request.use(req.resolve, req.reject)
// Add a response interceptor
http.interceptors.response.use(res.resolve, res.reject)
ajaxInvokeFileHttp.interceptors.response.use(req.resolve, req.reject)
ompHttp.interceptors.response.use(res.resolve, res.reject)
loghttp.interceptors.response.use(res.resolve, res.reject)
componentHttp.interceptors.response.use(req.resolve, req.reject)
fileDownHttp.interceptors.response.use(res.resolve, res.reject)
let setToken = function (token) {
  http.defaults.headers.common['token'] = token
}
let setOmpToken = function (token) {
  ompHttp.defaults.headers.common['token'] = token
}
let setComponentToken = function (token) {
  componentHttp.defaults.headers.common['token'] = token
}
let setAjaxInvokeFileToken = function (token) {
  ajaxInvokeFileHttp.defaults.headers.common['token'] = token
}
let setFileDownHttpToken = function (token) {
  fileDownHttp.defaults.headers.common['token'] = token
}
export {
  http,
  loghttp,
  setToken,
  setOmpToken,
  setComponentToken,
  setAjaxInvokeFileToken,
  setFileDownHttpToken,
  ompHttp,
  componentHttp,
  ajaxInvokeFileHttp,
  fileDownHttp
}
